package com.lujianfei.powersearch.ui.createOrUpdate.adapter

import androidx.databinding.BindingAdapter
import androidx.databinding.ObservableArrayList
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lujianfei.powersearch.R
import com.lujianfei.powersearch.base.BaseDatabindingAdapter
import com.lujianfei.powersearch.database.SearchItemModel
import com.lujianfei.powersearch.databinding.AdapterAppListBinding
import com.lujianfei.powersearch.ui.createOrUpdate.model.AppInfoBean

class AppSelectAdapter(items: ObservableArrayList<AppInfoBean>) :BaseDatabindingAdapter<AppInfoBean, AdapterAppListBinding>(
    items
) {

    var onItemClick: ((AppInfoBean) -> Unit)? = null

    override fun getLayoutResId() = R.layout.adapter_app_list

    override fun onBindItem(binding: AdapterAppListBinding?, bean: AppInfoBean, position: Int) {
        binding?.model = bean
        binding?.root?.setOnClickListener {
            onItemClick?.invoke(bean)
        }
    }
}


@BindingAdapter(value = ["bindAppSelect","onItemClick"], requireAll = false)
fun bindAppSelect(
    recyclerView: RecyclerView,
    items: ObservableArrayList<AppInfoBean>,
    onItemClick: ((AppInfoBean) -> Unit)? = null,
) {
    if (recyclerView.adapter != null) return

    val mAdapter = AppSelectAdapter(items)
    mAdapter.onItemClick = onItemClick
    recyclerView.apply {
        layoutManager = LinearLayoutManager(recyclerView.context)
        adapter = mAdapter
    }
}
